﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using BusinessBingo.CommandHandlers.Interfaces;
using BusinessBingo.CommandHandlers.Results;
using BusinessBingo.Commands;
using BusinessBingo.Model;
using BusinessBingo.Model.Interfaces;
using BusinessBingo.Queries;
using BusinessBingo.QueryHandlers;
using BusinessBingo.QueryHandlers.Interfaces;
using BusinessBingo.QueryHandlers.Results;
using BusinessBingo.Web.Bindings;
using BusinessBingo.Web.Controllers.Base;
using BusinessBingo.Web.Helpers;
using BusinessBingo.Web.Infrastructure.InfoStores.Interfaces;
using BusinessBingo.Web.Infrastructure.InfoStores.Keys;
using BusinessBingo.Web.Models.JsonResultModels;
using BusinessBingo.Web.Models.ViewModels;

namespace BusinessBingo.Web.Controllers
{
    public class HomeController : BaseController
    {
        private ICurrentLanguageStore _currentLanguageStore;
        private IGetRandomBingoSetQueryHandler _getRandomBingoSetQueryHandler;
        private IGetBingoSetQueryHandler _getBingoSetQueryHandler;

        private int _bingoSetSize = 4;

        public HomeController(
            IValidationState validationState,
            ICurrentLanguageStore currentLanguageStore, 
            IGetRandomBingoSetQueryHandler getRandomBingoSetQueryHandler,
            IGetBingoSetQueryHandler getBingoSetQueryHandler)
            : base(validationState)
        {
            this._currentLanguageStore = currentLanguageStore;
            this._getRandomBingoSetQueryHandler = getRandomBingoSetQueryHandler;
            this._getBingoSetQueryHandler = getBingoSetQueryHandler;
        }

        public ActionResult Index(CurrentUserData cookieData)
        {
            HomeIndexViewModel model = new HomeIndexViewModel();

            BingoSet bingoSet;
            if (cookieData == default(CurrentUserData))
            {
                LanguageKey language = this._currentLanguageStore.GetPreferredLanguage();

                GetRandomBingoSetQuery getRandomBingoSetQuery = new GetRandomBingoSetQuery(this._bingoSetSize, language);
                GetRandomBingoSetQueryHandlerResult getRandomBingoSetQueryHandlerResult =
                    this._getRandomBingoSetQueryHandler.Execute(getRandomBingoSetQuery);
                bingoSet = getRandomBingoSetQueryHandlerResult.BingoSet;
            }

            else
            {
                GetBingoSetQuery getBingoSetQuery = new GetBingoSetQuery(cookieData.UserId, cookieData.GameId);
                GetBingoSetQueryHandlerResult getBingoSetQueryHandlerResult =
                    this._getBingoSetQueryHandler.Execute(getBingoSetQuery);
                bingoSet = getBingoSetQueryHandlerResult.BingoSet;
                bingoSet.Size = this._bingoSetSize;
            }

            if (this._validationState.IsValid == false)
            {
                return View("Error");
            }

            model.BingoSetViewModel = ViewModelConverter.GetBingoSetViewModel(bingoSet);

            return View(model);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
